import random

import requests
import time


from lxml import etree


login_url = "https://login.zhipin.com/"
# url = "https://www.zhipin.com/c101020100/y_6/?query=java&page=1"
url = "https://www.zhipin.com/c101020100/?query=python%E5%AE%9E%E4%B9%A0&page=1&ka=page-2"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
    'cookie': 'lastCity=101020100; _bl_uid=wbkeh92pj15p258z04gsz36n3Fqe; __c=1593249664; __g=sem; __zp__pub__=; __l=l=%2Fwww.zhipin.com%2Fsem%2F10.html%3Fsid%3Dsem%26qudao%3Dbdpc_baidu-pc-BOSS-JD02-B19KA02084%26plan%3D%25E8%25A1%258C%25E4%25B8%259A%25E5%25AE%259A%25E6%258A%2595-%25E7%25AB%259E%25E5%2593%2581%26unit%3D%25E6%2599%25BA%25E8%2581%2594-2%26keyword%3Dwww.hugoboss.cn%26bd_vid%3D10603418226142070815%26csource%3Dboctb&r=https%3A%2F%2Fwww.baidu.com%2Fbaidu.php%3Fsc.a00000KEJeCxDFezE-oKym3FLnNdOd43meaMXztriSZUMtUjBWppV_JzrkQuODzxiWaLIK_8ADXB9yTbbZ_Jy5leBXrPRgYXd1c_zvRq0JxQsgob7sDIzAusQFy0WkHTQVpzhvkzk0RUc7C_8FeFNQ0jPYc0RbuJ3-J7_LCZ3rm1D_GsjosoIpfgNxYGYpADW7TYuMvbfqFK2yFlc8quhCrkFbIe.7b_NR2Ar5Od663rj6t8AGSPticrZA1AlaqM766WHGek3hcYlXE_sgn8mE8kstVerQKMks4OgSWS534Oqo4yunOogEOtZV_zyUr1oWC_knmx5u9qVXZutrZ1en5o_seOU9tqvZvSXZxeT5MY3IMVseqvxj4SrZvS8Zx43x5I9LtTrzEo6BxfmsSxH9Let5oeT5M_se2trO_14rEpMwsrh8m3rj6tJ_AGdeC3SHojAeKPa-BqYPaOonh4tzmpj7Na9WWOqmcrj635Q9JxWxDBaPrha81h_HAl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ww.baidu.com%26sht%3D50000021_hao_pg%26us%3D1.0.1.0.1.314.0%26ie%3Dutf-8%26f%3D8%26srcqid%3D4327514438385593050%26tn%3D50000021_hao_pg%26wd%3Dboss%26oq%3Dbosss%26rqlang%3Dcn%26inputT%3D199%26bc%3D110101&g=%2Fwww.zhipin.com%2Fsem%2F10.html%3Fsid%3Dsem%26qudao%3Dbdpc_baidu-pc-BOSS-JD02-B19KA02084%26plan%3D%25E8%25A1%258C%25E4%25B8%259A%25E5%25AE%259A%25E6%258A%2595-%25E7%25AB%259E%25E5%2593%2581%26unit%3D%25E6%2599%25BA%25E8%2581%2594-2%26keyword%3Dwww.hugoboss.cn%26bd_vid%3D10603418226142070815%26csource%3Dboctb&friend_source=0&friend_source=0; JSESSIONID=""; t=RhVwLzfy1MvqL95h; wt=RhVwLzfy1MvqL95h; __zp_seo_uuid__=10eb766f-c6cb-4f90-9cb0-0ed1599cff19; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1591588570,1591588698,1593249664,1593600894; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1593601128; __a=8132305.1588065488.1591576893.1593249664.144.7.86.86; __zp_stoken__=d96daWHdwQUIqc3R%2FQhdQE3lEbVBpd2wZAApoHBEFFQhLHxwQNBN1EiMEax1CFgZ2Jl0JQCYSd0cuHUosVCcAQERLCwUOcj1DdRoDTXNnY3kGOnZ7ZA1%2BP3hYGBxbUStxZGRMP3UbdmQ4ITk%3D; __zp_sseed__=XGugXo88seYiihFm2H7unh+nlI0oxu6N8XeMEVS70cM=; __zp_sname__=ce28664c; __zp_sts__=1593601250996'
}
free_proxy = [
    {"http": "http://113.120.62.46:9999"},
    {"http": "http://223.241.22.237:30001"},
    {"http": "http://60.188.12.39:3000"},
    {"http": "http://112.95.23.129:8888"},
    {"http": "http://182.88.117.185:9797"},
    {"http": "http://139.199.250.226:808"},
    {"http": "http://112.95.23.136:8888"},
    {"http": "http://183.129.207.87:12844"}
]

# 获取详情页url
def get_detail_urls():
    response = requests.get(url, headers=headers,proxies=random.choice(free_proxy))
    text = response.content.decode('utf-8')
    html = etree.HTML(text)
    ul = html.xpath('//div[@class="job-list"]/ul//li//div/@href')
    detail_url_list = []
    for detail_url in ul:
        base_url = 'https://www.zhipin.com'
        new_detail_url = base_url + detail_url
        detail_url_list.append(new_detail_url)

    return detail_url_list


# 获取详情页数据
def get_detail_data():

    get_data = get_detail_urls()
    job_data_list = []
    for get_url in get_data:
        job_data_dict = {}
        time.sleep(1)
        response = requests.get(get_url, headers=headers)
        text = response.content.decode('utf-8')
        html = etree.HTML(text)
        job_data_dict['职位名称'] = html.xpath("//div[@class='info-primary']//div[@class='name']//h1/text()")
        job_data_dict['薪资'] = html.xpath("//div[@class='info-primary']//div[@class='name']//span[@class='salary']/text()")
        job_data_dict['职位信息'] = html.xpath("//div[@class='job-sec']//div[@class='text']/text()")
        job_data_dict['公司名称'] = html.xpath("//div[@class='job-sec']//div[@class='name']/text()")
        job_data_dict['法人代表'] = html.xpath("//div[@class='job-sec']//div[@class='level-list']//li[1]/text()")
        job_data_dict['注册资金'] = html.xpath("//div[@class='job-sec']//div[@class='level-list']//li[2]/text()")
        job_data_dict['成立时间'] = html.xpath("//div[@class='level-list']//li[@class='res-time']/text()")
        job_data_dict['地址'] = html.xpath("//div[@class='location-address']/text()")
        job_data_list.append(job_data_dict)

    return job_data_list


if __name__ == '__main__':
    ret = get_detail_data()
    print(ret)





